package top.gytf.unifiedcall.common.support;

import lombok.extern.slf4j.Slf4j;
import top.gytf.unifiedcall.common.detial.AbstractUnifiedCallPackageLoader;
import top.gytf.unifiedcall.model.detail.UnifiedCallPackageDetail;

import java.util.HashSet;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 基于SPI的统一调用包加载器
 */
@Slf4j
public class SpiUnifiedCallPackageLoader extends AbstractUnifiedCallPackageLoader {
    private final Map<String, UnifiedCallPackageDetail> loaded;

    public SpiUnifiedCallPackageLoader() {
        loaded = ServiceLoader.load(UnifiedCallPackageDetail.class).stream()
                .map(ServiceLoader.Provider::get)
                .collect(Collectors.toMap(UnifiedCallPackageDetail::getName, Function.identity()));
        log.debug("完成通过SPI加载UnifiedCallPackage：{}", loaded.keySet());
    }

    @Override
    protected Set<UnifiedCallPackageDetail> doLoad() {
        return new HashSet<>(loaded.values());
    }
}
